local Screen = require "widgets/screen"
local Widget = require "widgets/widget"
local ImageButton = require "widgets/imagebutton"
local TEMPLATES = require "widgets/redux/templates"
local Image = require "widgets/image"
local Text = require "widgets/text"
local t_util = require "libs/tableutil"
local s_util = require "libs/settingmanager"

local writeablescreen
local WriteableWidget = require "screens/TMIP_writeablewidget"
local writeable_widget_args = {
    animbank = "ui_board_5x3",
    animbuild = "ui_board_5x3",
    menuoffset = Vector3(6, -70, 0),
    cancelbtn = {
        text = STRINGS.UI.TRADESCREEN.CANCEL,
        cb = function()end,
        control = CONTROL_CANCEL
    },
    acceptbtn = {
        text = STRINGS.UI.TRADESCREEN.ACCEPT,
        cb = function(w, meta)
            meta.fn(tonumber(w:GetText()), meta.btn)
        end,
        control = CONTROL_ACCEPT
    }
}
local function OpenWriteScreen(meta)
    if writeablescreen then
        writeablescreen:KillAllChildren()
        writeablescreen:Kill()
        writeablescreen = nil
    end
    writeablescreen = WriteableWidget(writeable_widget_args, meta)
    ThePlayer.HUD:OpenScreenUnderPause(writeablescreen)
    if TheFrontEnd:GetActiveScreen() == writeablescreen then
        writeablescreen.edit_text:SetEditing(true)
    end
end

local ScreenScreen = Class(Screen, function(self, sysdata)
    Screen._ctor(self, "EctrlScreen")

    local black = self:AddChild(ImageButton("images/global.xml", "square.tex"))
    black:SetOnClick(function() TheFrontEnd:PopScreen() end)
    local bg = black.image
    bg:SetVAnchor(ANCHOR_MIDDLE)       -- 默认在屏幕左下角
    bg:SetHAnchor(ANCHOR_MIDDLE)
    bg:SetScaleMode(SCALEMODE_FILLSCREEN)
    bg:SetTint(0,0,0,0)    -- 设置透明

    local book = self:AddChild(Widget("root"))
    book:SetVAnchor(ANCHOR_MIDDLE)
    book:SetHAnchor(ANCHOR_MIDDLE)
    local w,h = 1000, 625
    book.bg = book:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_menu_bg.tex"))
    book.bg:SetSize(w, h)
    book.title = book:AddChild(Text(HEADERFONT, 45, "霸气的装备管理", UICOLOURS.BLACK))
    book.title:SetPosition(0, h/2-45)
    book.title_line = book.title:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_line_break.tex"))
    book.title_line:SetPosition(0, -40)
    book.bar = book:AddChild(Image("images/quagmire_recipebook.xml", "quagmire_recipe_scroll_bar.tex"))
    book.bar:SetSize(5, h-240)
    book.bar:SetPosition(0, -40)
    local lines = book:AddChild(Widget("lines"))
    lines:SetPosition(-w/2+120, h/2-200)

    local i = 0
    for _, b_data in pairs(sysdata)do
        i = i+1
        local btn = lines:AddChild(ImageButton("images/quagmire_recipebook.xml", "cookbook_known.tex"))
        local btn_size = btn:GetSize()
        local btn_scale = 60/btn_size
        btn:SetNormalScale(btn_scale)
        btn:SetFocusScale(btn_scale*1.2)
        btn.img_or = btn:AddChild(Image("images/shroomcake_moreicons.xml", b_data.value and "right.tex" or "wrong.tex"))
        btn.img_or:SetSize(45, 45)
        btn:SetPosition((i-1)%2 * (w/2-60), -math.floor((i-1)/2)*70)
        btn:SetOnClick(function()
            if b_data.openfn then
                OpenWriteScreen({
                    fn = b_data.openfn,
                    btn = btn,
                })
            else
                b_data.fn(btn)
            end
        end)
        btn.labeltext = btn:AddChild(Text(HEADERFONT, 40, b_data.label, UICOLOURS.BLACK))
        btn.labeltext:SetPosition(150, 0)
        btn:SetHoverText(b_data.hover, {font = NEWFONT_OUTLINE, offset_y = b_data.openfn and -100 or 90 })
    end
end)


function ScreenScreen:OnControl(control, down)
    if ScreenScreen._base.OnControl(self, control, down) then return true end

    if not down and (control == CONTROL_MAP or control == CONTROL_CANCEL) then
		TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/click_move")
        TheFrontEnd:PopScreen()
        return true
    end
end

return ScreenScreen